package com.wuyou.simple;

/**
 * 69. x 的平方根
 */
public class MySqrt {


    public static void main(String[] args) {
        int x = 8;


        System.out.println(sol_1(x));
    }

    public static int sol_1(int x) {
        int l = 0, r = Integer.MAX_VALUE, mid = r / 2, res = 0;
        while (l <= r) {
            long mul = (long) mid * mid;
            long DeMul = (long) (mid - 1) * (mid - 1);
            if (DeMul < x && mul >= x) {
                res = mul == x ? mid : mid - 1;
                break;
            }
            if (mul > x) {
                r = mid - 1;
                mid = ((r - l) / 2) + l;
            } else {
                l = mid + 1;
                mid = ((r - l) / 2) + l;
            }
        }
        return res;
    }
}
